package weaver.interfaces.zhlx.duss.action.hrmresourcemodule.hcm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.HcmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 公司内部调动申请流程调用接口异动流程传输SAP
 * @ModifyLog:
 **/
public class WeaGsnbddsqlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaGsnbddsqlcAction>>>>>requestid=" + requestid);

        try {
            String message = "调用HCM系统接口返回:";
            RecordSet rs = new RecordSet();
            HcmUtil hcmUtil = new HcmUtil();
            String currentDate = DateUtil.getCurrentDate();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String ddrygh = Util.null2String(rs.getString("ddrygh"));
            String ddrq = Util.null2String(rs.getString("ddrq"));
            String ddlx1 = Util.null2String(rs.getString("ddlx1"));
            String sjyy = Util.null2String(rs.getString("sjyy"));
            String ddhrsfw = Util.null2String(rs.getString("ddhrsfw"));
            String ddhrszfw = Util.null2String(rs.getString("ddhrszfw"));
            String ddhygz = Util.null2String(rs.getString("ddhygz"));
            String ddhygzz = Util.null2String(rs.getString("ddhygzz"));
            String ddhlbbj = Util.null2String(rs.getString("ddhlbbj"));
            String gzfwh = Util.null2String(rs.getString("gzfwh"));
            String ddhgw = Util.null2String(rs.getString("ddhgw"));
            String ddhhtzt = Util.null2String(rs.getString("ddhhtzt"));
            String ddhsbzt = Util.null2String(rs.getString("ddhsbzt"));
            String ddhgzhszt = Util.null2String(rs.getString("ddhgzhszt"));
            String ddhzj = Util.null2String(rs.getString("ddhzj"));
            String ddhsrl = Util.null2String(rs.getString("ddhsrl"));
            String ddhtclrylb = Util.null2String(rs.getString("ddhtclrylb"));
            String ddhstd = Util.null2String(rs.getString("ddhstd"));
            String ddhcj = Util.null2String(rs.getString("ddhcj"));
            String ddhgwzt = Util.null2String(rs.getString("ddhgwzt"));
            String syqzth = Util.null2String(rs.getString("syqzth"));
            JSONArray dataList = new JSONArray();
            JSONObject dataJson = new JSONObject();
            dataJson.put("UUID", hcmUtil.getUUID16Str());
            dataJson.put("PERNR", ddrygh);
            boolean flag = hcmUtil.IsGrethanCurDate(ddrq, currentDate);
            String begda = hcmUtil.formatDate(ddrq);
            if (!flag) {
                begda = hcmUtil.formatDate(currentDate.substring(0, 7))+ "01";
            }
            dataJson.put("BEGDA", begda);
            dataJson.put("MASSN", ddlx1);
            dataJson.put("MASSG", sjyy);
            dataJson.put("WERKS", ddhrsfw);
            dataJson.put("BTRTL", ddhrszfw);
            dataJson.put("PERSG", ddhygz);
            dataJson.put("PERSK", ddhygzz);
            dataJson.put("ZTERF", ddhlbbj);
            dataJson.put("ABKRS", gzfwh);
            dataJson.put("PLANS", ddhgw);
            dataJson.put("SACHP", ddhhtzt);
            dataJson.put("SACHZ", ddhsbzt);
            dataJson.put("SACHA", ddhgzhszt);
            dataJson.put("ZZGWZD", ddhzj);
            dataJson.put("ZHR_SRL", ddhsrl);
            if ("Y6".equals(ddhgwzt) || "Y7".equals(ddhgwzt)) {
                dataJson.put("ZZGWZT", ddhgwzt);
            }
            dataJson.put("ZZTCLRYLB", ddhtclrylb);
            dataJson.put("ZHR_STD", ddhstd);
            dataJson.put("ZHR_ZWCJ", ddhcj);
            dataJson.put("ZZSYZT", syqzth);
            dataList.add(dataJson);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaGsnbddsqlcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaGsnbddsqlcAction>>>>>dataList=" + dataList);
            JSONObject resultJson = hcmUtil.handleOKHttpRequest("EC201", dataList, dataMap);
            writeLog(">>>>>WeaGsnbddsqlcAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                if (!"S".equals(msgType)) {
                    message += outputJson.getString("MSG_TEXT");
                    String returnData = outputJson.getString("DATA");
                    JSONArray returnDataList = JSONArray.parseArray(returnData);
                    for (int i = 0; i < returnDataList.size(); i++) {
                        String type = returnDataList.getJSONObject(i).getString("TYPE");
                        if ("E".equalsIgnoreCase(type)) {
                            message += returnDataList.getJSONObject(i).getString("MESSAGE");
                        }
                    }
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaGsnbddsqlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}